package com.hjt.frame.panels;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import com.hjt.dto.MemberInfo;
import com.hjt.frame.MainFrame;

public class RoleLabel extends MouseAdapter{
	private JFrame frame;
	private RolePanel panel;
	private Double textAreaNum;
	private MemberInfo selected;
	private Map<String, JLabel> labelMap = new HashMap<>();
	private Map<String, MemberInfo> labelMemberMap = new HashMap<>();
	public RoleLabel(JFrame frame, RolePanel panel, Double textAreaNum) {
		this.frame = frame;
		this.panel = panel;
		this.textAreaNum = textAreaNum;
		findRoles();
		panel.setData(selected);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		JLabel label = (JLabel)e.getSource();
		JLabel oldLabel = labelMap.get(selected.getLabel());
		selected = labelMemberMap.get(label.getText());
		label.repaint();
		oldLabel.repaint();
		panel.setData(selected);
	}
	
	private Map<String, JLabel> findRoles() {
		Map<String, JLabel> map = new LinkedHashMap<>();
		int num = 0;
		for(MemberInfo m: MainFrame.members) {
			if(num == 0) {
				selected = m;
			}
			JLabel label = new JLabel(m.getLabel(), SwingConstants.CENTER) {
				@Override
				public void paint(Graphics g) {
					if(selected.getLabel().equals(this.getText())) {
						g.setColor(Color.WHITE);
						g.fillRect(0, 0, this.getWidth(), this.getHeight());
						super.paint(g);
					}else {
						g.setColor(new Color(245, 247, 250));
						g.fillRect(0, 0, this.getWidth(), this.getHeight());
						super.paint(g);
						g.setColor(new Color(151, 151, 151));
						g.drawLine(1, 1, 1, this.getHeight() - 1);
						g.drawLine(1, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1);
					}
				}
			};
			label.setBackground(Color.white);
			label.setBounds(10 + 80 * num, 60 + Double.valueOf(textAreaNum * 30).intValue(), 80, 30);
			label.addMouseListener(this);
			labelMap.put(label.getText(), label);
			labelMemberMap.put(label.getText(), m);
			this.frame.add(label);
			num++;
		}
		return map;
	}
	
}
